package com.tzsp.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tzsp.reggie.commons.contans.R;
import com.tzsp.reggie.domain.Category;
import com.tzsp.reggie.domain.Employee;
import com.tzsp.reggie.service.CategoryService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/category")
@Slf4j
public class CategoryController {
    @Autowired
    private CategoryService categoryService;

    /**
     * 新增菜品分类&&新增套餐分类
     * @param category
     * @return
     */
    @PostMapping
    public R<String> AddDishCategory(@RequestBody Category category){
        categoryService.save(category);
        return R.success("新增菜品成功");
    }

    /**
     * 分页查询菜品
     * @param page
     * @param pageSize
     * @param
     * @return
     */
    @GetMapping("/page")
    public R<Page> queryDishCategoryInfoByPage(int page, int pageSize){
        log.info("page={},pageSize={},name={}",page,pageSize);

        //构造分页构造器
        Page<Category> pageInfo = new Page<>(page,pageSize);
        //构造条件构造器
        LambdaQueryWrapper<Category> lqw = new LambdaQueryWrapper<>();
        //添加过滤条件，
        //lqw.like(StringUtils.isNotEmpty(name),Employee::getName,name);
        //添加排序条件
        lqw.orderByAsc(Category::getSort);
        //执行 查询
        categoryService.page(pageInfo,lqw);

        return R.success(pageInfo);
    }

    /**
     * 根据id删除菜品分类
     * @param ids
     * @return
     */
    @DeleteMapping
    public R<String> deleteDishCategory(Long ids){
        log.info("id:{}",ids);
        //categoryService.removeById(ids);
        categoryService.deleteDishCategory(ids);
        return R.success("删除成功");
    }

    /**
     * 修改菜品分类
     * @param category
     * @return
     */
    @PutMapping
    public R<String> updataDishCategory(@RequestBody Category category){
        categoryService.updateById(category);
        return R.success("修改菜品分类成功");
    }
    /**
     * 获取菜品分类
     * @param category
     * @return
     */
    @GetMapping("/list")
    public R<List<Category>> listDishCategory(Category category){
        //条件构造器
        LambdaQueryWrapper<Category> queryWrapper = new LambdaQueryWrapper<>();
        //添加条件
        queryWrapper.eq(category.getType()!=null,Category::getType,category.getType());
        //添加排序条件
        queryWrapper.orderByAsc(Category::getSort).orderByDesc(Category::getUpdateTime);


        //查询
        List<Category> categoryList = categoryService.list(queryWrapper);

        return R.success(categoryList);
    }
}
